<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="名称" prop="auth_name">
        <el-input v-model="form.auth_name" />
      </el-form-item>
      <el-form-item label="父级" prop="auth_pid">
        <el-select
          v-model="form.auth_pid"
          placeholder="请选择父级权限"
          clearable
        >
          <el-option
            v-for="auth in auths"
            :key="auth.id"
            :value="auth.id"
            :label="auth.auth_name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="控制器" prop="auth_c">
        <el-input v-model="form.auth_c" />
      </el-form-item>
      <el-form-item label="方法" prop="auth_a">
        <el-input v-model="form.auth_a" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getList, getDetail, doAdd, doEdit } from '@/api/auth'

  export default {
    name: 'AuthEdit',

    data() {
      return {
        form: {
          auth_name: '',
          auth_pid: null,
          auth_c: '', // Index
          auth_a: '', // index
        },
        rules: {
          auth_name: [
            { required: true, trigger: 'blur', message: '请输入权限名称' },
          ],
          auth_c: [
            { required: true, trigger: 'blur', message: '请输入控制器名称' },
          ],
          auth_a: [
            { required: true, trigger: 'blur', message: '请输入方法名称' },
          ],
        },
        auths: [],
        title: '',
        dialogFormVisible: false,
        type: '',
      }
    },

    created() {
      this.initList()
    },

    methods: {
      initList() {
        getList({ list_row: 9999 }).then((res) => {
          this.auths = res.data
        })
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.type = 'add'
        } else {
          this.title = '编辑'
          this.type = 'edit'
          getDetail({ auth_id: row.id }).then((res) => {
            this.form = {
              ...res,
              auth_pid: res.auth_pid || null,
            }
          })
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let msg
            if (this.type === 'add') msg = await doAdd(this.form)
            else msg = await doEdit({ ...this.form, auth_id: this.form.id })
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
